﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

namespace POC
{
    static class AddressEntryDAO
    {
        public static AddressEntry LastClicked = new AddressEntry();
        public static List<AddressEntry> LoadAll()
        {
            var request = WebRequest.Create("https://csnyd042-poc.appspot.com/svc/list?");
            string text = "";
            request.ContentType = "application/json;";
            var response = (HttpWebResponse)request.GetResponse();

            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                text = sr.ReadToEnd();
            }
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<AddressEntry> list = serializer.Deserialize<List<AddressEntry>>(text);
            response.Close();
            return list;
        }
        public static void Update(AddressEntry entry)
        {
            string requestString = "https://csnyd042-poc.appspot.com/svc/update?"
                + "id=" + LastClicked.ID
                + "&name=" + entry.Name
                + "&email=" + entry.Email;
            var request = WebRequest.Create(requestString);
            var response = (HttpWebResponse)request.GetResponse();
            response.Close();
        }
        public static void Delete()
        {
            string requestString = "https://csnyd042-poc.appspot.com/svc/delete?"
                + "id=" + LastClicked.ID
                + "&name=" + LastClicked.Name
                + "&email=" + LastClicked.Email;
            var request = WebRequest.Create(requestString);
            var response = (HttpWebResponse)request.GetResponse();
            response.Close();
        }
        public static void Add(AddressEntry entry)
        {
            string requestString = "https://csnyd042-poc.appspot.com/svc/add?"
                + "name=" + entry.Name
                + "&email=" + entry.Email;
            var request = WebRequest.Create(requestString);
            var response = (HttpWebResponse)request.GetResponse();
            response.Close();
        }

    }
}
